package com.interceptor;

import com.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//1
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("UserLoginInterceptor");
        //得到session
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("user");
        System.out.println(user);
        // session存在时，放行
        if(user!=null){
            return true;
        }
        // 执行这里表示用户身份需要认证，跳转登陆页面
//        response.sendRedirect("http://localhost:8082/dist/index.html#/user");
//        response.sendRedirect("https://www.baidu.com/");
//        request.getRequestDispatcher("https://www.baidu.com/").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
